package com.quanyan.user.service.admin.impl;

import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.user.mapper.UserInterestAdminMapper;
import com.quanyan.user.model.vo.request.admin.ReqTouristInterestQuery;
import com.quanyan.user.model.vo.request.admin.ReqUserInterestQuery;
import com.quanyan.user.model.vo.response.admin.RespAdminUserInterest;
import com.quanyan.user.model.vo.response.admin.RespTouristInterest;
import com.quanyan.user.service.admin.UserInterestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by hua on 2016/8/19.
 */
@Service
public class UserInterestServiceImpl implements UserInterestService {
    private final static Logger logger = LoggerFactory.getLogger(UserInterestServiceImpl.class);
    @Autowired
    private UserInterestAdminMapper userInterestAdminMapper;

    @Override
    public PageObj<List<RespAdminUserInterest>> queryUserInterestList(ReqUserInterestQuery userInterestQuery) {
        List<RespAdminUserInterest> userInterests = null;
        int pageNo = userInterestQuery.getPageNo();
        int total = 0;
        try {
            userInterestQuery.setPageNo((userInterestQuery.getPageNo() - 1) * userInterestQuery.getPageSize());
            if (userInterestQuery.getStartTime() != null) {
                userInterestQuery.setStartTime(DateUtils.dayBegin(userInterestQuery.getStartTime()));
            }
            if (userInterestQuery.getEndTime() != null) {
                userInterestQuery.setEndTime(DateUtils.dayEnd(userInterestQuery.getEndTime()));
            }
            userInterests = userInterestAdminMapper.queryUserInterestList(userInterestQuery);
            total = userInterestAdminMapper.selectCountByQuery(userInterestQuery);
        } catch (Exception e) {
            logger.error("获取用户兴趣列表失败！", e);
        }
        return PageObj.create(total, pageNo, userInterestQuery.getPageSize(), userInterests);
    }

    @Override
    public PageObj<List<RespTouristInterest>> queryTouristInterestList(ReqTouristInterestQuery touristInterestQuery) {
        List<RespTouristInterest> touristInterests = null;
        int pageNo = touristInterestQuery.getPageNo();
        int total = 0;
        try {
            touristInterestQuery.setPageNo((touristInterestQuery.getPageNo() - 1) * touristInterestQuery.getPageSize());
            if (touristInterestQuery.getStartTime() != null) {
                touristInterestQuery.setStartTime(DateUtils.dayBegin(touristInterestQuery.getStartTime()));
            }
            if (touristInterestQuery.getEndTime() != null) {
                touristInterestQuery.setEndTime(DateUtils.dayEnd(touristInterestQuery.getEndTime()));
            }
            touristInterests = userInterestAdminMapper.queryTouristInterestList(touristInterestQuery);
            total = userInterestAdminMapper.selectTouristCountByQuery(touristInterestQuery);
        } catch (Exception e) {
            logger.error("获取游客兴趣列表失败！", e);
        }
        return PageObj.create(total, pageNo, touristInterestQuery.getPageSize(), touristInterests);
    }
}
